package com.ww.springboot.mybatis.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.common.collect.Lists;
import com.ww.springboot.mybatis.mapper.TestMapper;
import com.ww.springboot.mybatis.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2023-05-05 14:49
 */
//@Component
public class TestConfig {

    @Autowired
    private TestMapper testMapper;

    @PostConstruct
    public void init() {
        ExcelReader reader = ExcelUtil.getReader(new File("C:\\Users\\ZH500\\桌面\\test\\导折扣(1).xlsx"));
        List<List<Object>> read = reader.read();
        List<String> list = new ArrayList<>();
        read.forEach(one -> {
            list.add((String) one.get(0));
        });
        System.out.println(list.size());

        List<List<String>> partition = Lists.partition(list, 500);

        StringBuilder stringBuilder = new StringBuilder();

        partition.forEach(one -> {
            List<Map<String, String>> test = testMapper.test(one);
            test.forEach(map -> {
                String s = map.get("field_value");
                if (ObjectUtil.isNotEmpty(s)) {
                    s = s.substring(2, s.length() - 2);
                }
                stringBuilder.append(map.get("product_sku") + "\t" + map.get("nw_sku") + "\t" + s + "\n");
            });
        });

        FileUtil.writeBytes(stringBuilder.toString().getBytes(), "C:\\Users\\ZH500\\桌面\\test\\导折扣(1) - 副本.txt");
    }
}
